package oa.service.impl;

import java.io.Serializable;
import java.util.Collection;
import java.util.ListIterator;
import java.util.Set;

import javax.annotation.Resource;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import oa.dao.DepartmentDao;
import oa.dao.RoleDao;
import oa.dao.UserDao;
import oa.domain.page.PageBean;
import oa.domain.system.Department;
import oa.domain.system.Role;
import oa.domain.system.User;
import oa.service.UserService;
import oa.util.HQLHelper;

@Service("userService")
public class UserServiceImpl implements UserService{

	@Resource(name="userDao")
	private UserDao userDao;
	@Resource(name="roleDao")
	private RoleDao roleDao;
	@Resource(name="departmentDao")
	private DepartmentDao departmentDao;
	@Override
	public Collection<User> getAllUsers() {
		// TODO Auto-generated method stub
		return this.userDao.getAllEntry();
	}

	@Override
	public User getUserById(Serializable id) {
		// TODO Auto-generated method stub
		return this.userDao.getEntryById(id);
	}

	
	@Override
	@Transactional(readOnly = false)
	public void deleteUser(Serializable id) {
		// TODO Auto-generated method stub
		this.userDao.deleteEntry(id);
	}

	@Override
	@Transactional(readOnly = false)
	public void saveUser(Long did, Long[] rids, User user) {
		// TODO Auto-generated method stub
		
		Set<Role> roles = this.roleDao.getEntryByIds(rids);
		Department department = this.departmentDao.getEntryById(did);
		
		user.setDepartment(department);
		user.setRoles(roles);
	
		this.userDao.saveEntity(user);
	}

	@Override
	@Transactional(readOnly = false)
	public void updateUser(Long did,Long[] rids, User user) {
		// TODO Auto-generated method stub
		Set<Role> roles = this.roleDao.getEntryByIds(rids);
		Department department = this.departmentDao.getEntryById(did);
		
		user.setDepartment(department);
		user.setRoles(roles);
	
		this.userDao.updateEntry(user);
	}

	@Transactional(readOnly=false)
	public void updateUser(User user) {
		// TODO Auto-generated method stub
		this.userDao.updateEntry(user);
	}

	@Override
	public PageBean getPageBean(HQLHelper hh, int currentPage) {
		// TODO Auto-generated method stub
		return this.userDao.getPageBean(hh, currentPage);
	}

}
